<template>
    <LDrawerForm 
        :visible="visible"  
        :confirmLoading="confirmLoading" 
        :title="title" ref="templateForm" 
        :formAttributes="formAttributes"
        :component="templateDetailList"
        :componentParam="componentParam"
        :width="1000"
         @handleOk="handleOk" 
         @handleCancel="handleCancel"
         @clickEventHandler="clickEventHandler"
         @changeEventHandler="changeEventHandler">
    </LDrawerForm>

</template>

<script>
import LDrawerForm from '@/components/lt/form/LDrawerForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import templateDetailList from './templateDetailList'

export default {
    name:'templateForm',
    components:{
        LDrawerForm,templateDetailList
    },
    mixins:[],
    data(){
        return{
            templateDetailList,
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            componentParam:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'templateId',label:'模板内码',component:'input',disabled:true},
                    {field:'templateName',label:'模板名称',component:'input',rules: [{ required: true, message: '请输入模板名称!' }]},
                    {field:'entityClassName',label:'实体名称',component:'inputSearch',rules: [{required: true, message: '请输入实体名称!'}]},
                    {field:'serviceClassName',label:'接口名称',component:'input',rules: [{required: true, message: '请输入接口名称!'}]},
                    {field:'importBeforeCallback',label:'导入前回调',component:'input'},
                    {field:'importAfterCallback',label:'导入后回调',component:'input'},
                    {field:'importRowCallback',label:'行回调',component:'input'},
                    
                    // {field:'importMode',label:'数据导入模式',component:'radio',hide:true,options:[{label: '先清空再添加', value: '1'},{label: '直接添加', value: '2'}],initialValue:'2'},
                  ]
                },
              ]

            }

            
        }
            
        
    },
    methods:{
        add () {
            this.edit({});
        },
        edit (record) {
            this.visible = true;
            this.model = Object.assign({}, record);
            this.componentParam={templateId:this.model['templateId']}
            this.$nextTick(() => {
                this.$refs.templateForm.getForm().setFieldsValue(pick(this.model,'templateId','templateName','entityClassName','serviceClassName','importBeforeCallback','importAfterCallback','importRowCallback'))
            })
        },
        handleOk(){
            const that = this;
            this.$refs.templateForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                    request({url: '/SysImportTemplateController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.close()
                            that.$message.success(res.message);
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    //    that.close()
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
         /**
         * 表单内容发生变化
         */
        changeEventHandler(changeItem){
     
        },
        clickEventHandler(field,form){
            let that = this
            if("entityClassName"==field){
               
               this.$refs.templateForm.getForm().validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true
                        let formData = Object.assign(this.model, values);
                        request({url: '/SysImportTemplateController/initEntityInfo',method: 'post',data: formData}).then((res)=>{
                            if(res.success){
                                that.componentParam={templateId:res.result|| res.message}
                                that.model['templateId'] = res.result|| res.message
                                that.$refs.templateForm.getForm().setFieldsValue({templateId:that.model['templateId']});
                                that.$message.success("执行成功");
                            }else{
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                        })
                        
                    }
                })
           }
        }

    }
}
</script>